
import {Form} from 'antd';
import {
  KfBackendEnumPicker,
  KfBizQuickFormProps,
  KfFloatingContextProps,
  KfInput,
  KfQuickForm,
  KfTextArea,
  DEFAULT_FORM_LAYOUT,
  KfGroupPicker
} from "kf-web-library";
import type {FunctionComponent} from 'react';
import React from 'react';
import * as API from "@/services";

const FormItem = Form.Item;
type QueryEntity = API.Group;
type CreateEntity = API.Group_Create;
type ModifyEntity = API.Group_Modify;

type DataFormProps = {} & KfBizQuickFormProps & KfFloatingContextProps;

/**
 * 组织表单
 */
const QuickForm: FunctionComponent<DataFormProps> = (props) => {
    const { formType, bizId, ...rest } = props;
    const extra = <div style={{ color: '#ccc' }}>例：103.xxxxxx,31.xxxxxx</div>;
    return (
        <KfQuickForm
            formType={formType}
            formProps={{
                ...DEFAULT_FORM_LAYOUT,
            }}
            defaultQueryRequest={() =>
              API.queryGroup(bizId!).then((resp) => {
                    if (resp.flag) {
                        resp.data = {
                            ...resp.data,
                            roleIds: resp.data.roles?.map((r) => r.id) || [],
                        } as API.Group_Modify;
                    }
                    return new Promise((resolve) => resolve(resp));
                })
            }
            submitRequest={{
                create: (formValues) => API.createGroup({ ...formValues } as CreateEntity),
                modify: (formValues) => API.modifyGroup({ ...formValues, id: bizId } as ModifyEntity),
            }}
            {...rest}
        >
            <FormItem name={nameof<ModifyEntity>((x) => x.parentId)} label="上级组织" rules={[{ required: false }]}>
              <KfGroupPicker.TreeSelect showFullLabel={true} excludeKeys={(bizId ? [bizId] : []) as string[]} />
            </FormItem>
            <FormItem name={nameof<ModifyEntity>((x) => x.name)} label="组织名称" rules={[{ required: true }]}>
                <KfInput />
            </FormItem>
            <FormItem name={nameof<ModifyEntity>((x) => x.type)} label="组织类型" rules={[{ required: true }]}>
                <KfBackendEnumPicker.RadioGroup enumType={'GroupType'} />
            </FormItem>
            {/* <FormItem name={nameof<SubmitEntity>(x => x.areaId)} label="所在地区" rules={[{ required: false }]}>
                <KfAreaPicker top={{ areaId: '5101', areaMaxLevel: 'CITY' }} />
            </FormItem> */}
            {/* <FormItem name={nameof<ModifyEntity>((x) => x.roleIds)} label="组织角色" rules={[{ required: false }]}>
                <KfRolePicker mode="multiple" />
            </FormItem> */}
            <FormItem name={nameof<ModifyEntity>((x) => x.description)} label="备注" rules={[{ required: false }]}>
                <KfTextArea />
            </FormItem>
        </KfQuickForm>
    );
};

export default QuickForm;
